package cn.im.qywx.wx.util;

import cn.im.qywx.view.WeiXinXml;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.thymeleaf.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;


/**
 * @Description: 解析微信消息
 */
public class AnalyzeWeiXinMsgXml extends AnalyzeXml {

	/**
	 * 解析用户发送的消息
	 * 
	 * @param xml
	 */
	public static void analyze(String xml, WeiXinXml wXml) {

		if (StringUtils.isEmpty(xml) || null == wXml) {
			wXml = null;
			return;
		}

		Element rootEle;
		String[] mustEleNames = { "ToUserName", "FromUserName", "MsgType" };// 必须存在的节点名
		String[] analyzeEleNames = { "AgentID","CreateTime", "Content",/* "Location_X", "Location_Y", "Scale", "Label",*/ "MsgId", "Event", "EventKey" ,"MediaId", "Recognition", "Ticket"};// 需要解析的节点名

		try {
			rootEle = getXmlReader().read(new ByteArrayInputStream(xml.getBytes("UTF-8"))).getRootElement();// RootNode

			// 判断必须并存在的节点名并设置对象对应值----------------------
			for (String mustEle : mustEleNames) {
				if (null != rootEle.element(mustEle) && !StringUtils.isEmpty(rootEle.element(mustEle).getStringValue().trim())) {
					setParam(wXml, mustEle, rootEle.element(mustEle).getStringValue().trim());
				} else {
					wXml = null;
					return;
				}
			}

			// 设需要解析的节点对象对应值-----------------------
			for (String analyzeEle : analyzeEleNames) {
				if(null != rootEle.element(analyzeEle) && !StringUtils.isEmpty(rootEle.element(analyzeEle).getStringValue().trim())){
					setParam(wXml, analyzeEle, rootEle.element(analyzeEle).getStringValue().trim());
				}
			}

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			wXml = null;
			return;
		} catch (DocumentException e) {
			e.printStackTrace();
			wXml = null;
			return;
		} catch (Exception e) {
			e.printStackTrace();
			wXml = null;
			return;
		}

	}
	

	/**
	 * 设置对象值
	 * 
	 * @param wXml 对象
	 * @param name 名称
	 * @param value 值
	 */
	private static void setParam(WeiXinXml wXml, String name, String value) {
		
		if(!StringUtils.isEmpty(value)){
			value = value.trim();
		}
		
		if ("ToUserName".equals(name)) {
			wXml.setToUserName(value);
		} else if ("FromUserName".equals(name)) {
			wXml.setFromUserName(value);
		} else if ("MsgType".equals(name)) {
			wXml.setMsgType(value);
		} else if ("CreateTime".equals(name)) {
			wXml.setCreateTime(value);
		} else if ("Content".equals(name)) {
			wXml.setContent(value);
		} /*else if ("Location_X".equals(name)) {
			wXml.setLocation_X(value);
		} else if ("Location_Y".equals(name)) {
			wXml.setLocation_Y(value);
		} else if ("Scale".equals(name)) {
			wXml.setScale(value);
		} else if ("Label".equals(name)) {
			wXml.setLabel(value);
		} */else if ("MsgId".equals(name)) {
			wXml.setMsgId(value);
		}else if ("AgentID".equals(name)) {
			wXml.setAgentId(value);
		} else if ("Event".equals(name)) {
			wXml.setEvent(value);
		} else if ("EventKey".equals(name)) {
			wXml.setEventKey(value);
		}else if("MediaId".equals(name)){
			wXml.setMediaId(value);
		}else if("Recognition".equals(name)){
			wXml.setRecognition(value);
		}else if("Ticket".equals(name)){
			wXml.setTicket(value);
		}

	}

}
